package com.fjnu.service.impl;

import com.fjnu.annotation.ExceptionGlobalCatch;
import com.fjnu.dao.UserDao;
import com.fjnu.dto.UserConditionDto;
import com.fjnu.entity.User;
import com.fjnu.service.UserService;
import com.fjnu.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户服务实现类
 */
@Service
@ExceptionGlobalCatch
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserDao userDao;
    
    @Override
    public User queryByName(String username) {
        // 创建一个临时的UserConditionDto来进行查询
        UserConditionDto condition = new UserConditionDto();
        condition.setUsername(username);
        List<User> users = userDao.selectByCondition(condition);
        return users.isEmpty() ? null : users.get(0);
    }
    
    @Override
    @Transactional
    public int register(User user) {
        // 检查必要字段
        if (user.getName() == null || user.getName().isEmpty()) {
            throw new IllegalArgumentException("用户名不能为空");
        }
        
        user.setCreateTime(LocalDateTime.now());
        int result = userDao.insert(user);
        
        // 如果插入失败，抛出异常以触发事务回滚
        if (result <= 0) {
            throw new RuntimeException("用户注册失败");
        }
        
        return result;
    }
    
    @Override
    public PageResult queryUsers(UserConditionDto userCondition, Integer pageNum, Integer pageSize) {
        // 计算起始索引
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = pageNum * pageSize;
        
        // 获取符合条件的用户列表
        List<User> users = userDao.selectByConditionWithLimit(userCondition, startIndex, endIndex);
        
        // 获取总记录数，可以通过查询所有符合条件的记录数获得
        List<User> allUsers = userDao.selectByCondition(userCondition);
        long total = allUsers.size();
        
        // 创建并返回PageResult对象
        return new PageResult(total, pageNum, pageSize, users);
    }
    
    @Override
    @Transactional
    public void addAssignRole(String userId, List<Integer> roleIds) {
        // 检查参数
        if (userId == null || userId.isEmpty()) {
            throw new IllegalArgumentException("用户ID不能为空");
        }
        
        if (roleIds == null || roleIds.isEmpty()) {
            throw new IllegalArgumentException("角色ID列表不能为空");
        }
        
        // 实现分配角色的逻辑
        for (Integer roleId : roleIds) {
            userDao.insertUserRole(Integer.parseInt(userId), roleId);
        }
    }
    
    @Override
    @Transactional
    public int changePassword(String userId, String newPassword) {
        if (userId == null || userId.isEmpty()) {
            throw new IllegalArgumentException("用户ID不能为空");
        }
        
        if (newPassword == null || newPassword.isEmpty()) {
            throw new IllegalArgumentException("新密码不能为空");
        }
        
        int result = userDao.updatePassword(Integer.parseInt(userId), newPassword);
        
        // 如果更新失败，抛出异常以触发事务回滚
        if (result <= 0) {
            throw new RuntimeException("修改密码失败");
        }
        
        return result;
    }
}